#!/bin/ksh

######################################################################
# This script is intended to be run on the first reboot of the HMC after
# its been installed using the Restore CD
#
# We have two situations:
#  1) First boot after a clean install from the Restore CD
#  2) First boot after a clean install from the Restore CD and a user data restore
#
#  In case (1), the /var/ct directory WILL NOT exist
#
#  In case (2), the /var/ct directory WILL exist, and it will contain these files:
#    /var/ct/cfg/ct_has.pkf
#    /var/ct/cfg/ct_has.qkf
#    /var/ct/cfg/ct_has.thl
#    /var/ct/cfg/ctrmc.acls
#    /var/ct/IW/registry/*
#
#  For case (1), we need to generate a new Node ID.
#  For case (2), we also need to generate a new Node ID since its not saved in the save/restore process,
#                         and we may need to migrate existing ctrmc.acls files and registry entries from GA2 to 
#                         GA3 format and data.
######################################################################
migrateRMCACL()
{

	# if the ctrmc.acls file is not present, copy the default one to /var/ct/cfg
	if [ ! -f /var/ct/cfg/ctrmc.acls ]
	then
		echo "copying /usr/sbin/rsct/cfg/ctrmc.acls file to /var/ct/cfg" >> /opt/hsc/data/RMC_first_boot_completed
		cp /usr/sbin/rsct/cfg/ctrmc.acls /var/ct/cfg/
	fi

	# its also possible that the /var/ct/cfg/ctrmc.acls file was saved from a GA2 HMC, 
	# in which case we will need to add a DEFAULT stanza to it.

	# Note - the stanza must start in the first column of the acl file, so we use the ^ in grep
 	x=`grep ^DEFAULT /var/ct/cfg/ctrmc.acls`
   	if [ $? -ne 0 ]
  	then
		echo "adding a DEFAULT stanza to /var/ct/cfg/ctrmc.acls" >> /opt/hsc/data/RMC_first_boot_completed
   	echo " " >> /var/ct/cfg/ctrmc.acls
   	echo "# This stanza added by $0. It is requred to be present - do not remove it" >> /var/ct/cfg/ctrmc.acls
   	echo "DEFAULT" >> /var/ct/cfg/ctrmc.acls
   	echo "    root@LOCALHOST           *          rw" >> /var/ct/cfg/ctrmc.acls
   	echo "    LOCALHOST                *          r" >> /var/ct/cfg/ctrmc.acls
  	fi

  	x=`grep ^IBM.LparCmd /var/ct/cfg/ctrmc.acls`
  	if [ $? -ne 0 ]
  	then
		echo "adding a IBM.LparCmd stanza to /var/ct/cfg/ctrmc.acls" >> /opt/hsc/data/RMC_first_boot_completed
   	echo " " >> /var/ct/cfg/ctrmc.acls
   	echo "# This stanza added by $0. It is requred to be present - do not remove it" >> /var/ct/cfg/ctrmc.acls
   	echo "IBM.LparCmd" >> /var/ct/cfg/ctrmc.acls
   	echo "         root@LOCALHOST      *    rw" >>/var/ct/cfg/ctrmc.acls
   	echo "         UNAUTHENT           *    r" >>/var/ct/cfg/ctrmc.acls    
  	fi

  	/opt/hsc/bin/setHSCGroup >/opt/hsc/data/RMC_first_boot_completed 2>&1
  	typeset -i GROUPID
  	for i in `cat /etc/passwd`
  	do
    	USERNAME=`echo $i | awk 'BEGIN {FS=":"} {print $1} END {}'`
    	GROUPID=`echo $i | awk 'BEGIN {FS=":"} {print $4} END {}'`
    	if [ $GROUPID -ge 500 ]; then
      		if [ $GROUPID -lt 507 ]; then
		  	echo "invoking /opt/hsc/bin/rmcadduser $USERNAME" >> /opt/hsc/data/RMC_first_boot_completed
        			/opt/hsc/bin/rmcadduser $USERNAME >>/opt/hsc/data/RMC_first_boot_completed 2>&1
      		fi
    	fi
  done
}

PATH=/usr/sbin/rsct/install/bin:/usr/sbin/rsct/bin:/usr/bin:/bin:/usr/sbin
export PATH
cd /
# If this file does not exist, then this script ran once before, so we quit now!
if [ ! -f /opt/hsc/data/.setupRMC ]
then
  exit 0
fi

echo "$0 started on "`date` > /opt/hsc/data/RMC_first_boot_completed

# setup /var/ct - this script will generate a new node id if one does not exist,
# create any missing directories under /var/ct/, etc.
# It then invokes rmcctrl -s, which starts ctrmc and maybe other daemons....
# /var/ct/lck and /var/ct/cfg are directories that require to exist
# otherwise cfgct will fail

if [ ! -d /var/ct/lck ]
then
  mkdir -p /var/ct/lck
fi
if [ ! -d /var/ct/cfg ]
then
  mkdir -p /var/ct/cfg
fi

# If IW exists as a physical directory
# rename it first

if [ -d /var/ct/IW -a ! -h /var/ct/IW ]
then
   mv /var/ct/IW /var/ct/orilIW

# If /var/ct/IW does not exists and a directory specified
# in /var/ct/cfg/clusters exists, then must come from
# a DVD restore

else
  if [ ! -e /var/ct/IW  ]
  then
    iwdir=`cat /var/ct/cfg/clusters | cut -d' ' -f 1`
    if [ "$iwdir" != "" -a  -d /var/ct/$iwdir ]
    then
      mv /var/ct/$iwdir /var/ct/orilIW
    fi
  fi
fi

# If we get to this point and there is 2 possible
# cases:
# to a unique named directory, cfgct should do the
# right thing. If There is no IW directory, a new
# one will be generated by cfgct
# First export INUCLIENTS to tell cfgct not to start rmc daemons

export INUCLIENTS=1

echo -n "running cfgct :" >> /opt/hsc/data/RMC_first_boot_completed
cfgct 2>&1 >> /opt/hsc/data/RMC_first_boot_completed

# unset $INUCLIENTS

unset INUCLIENTS

# stop all daemons
rmcctrl -z

# at this point, rsct is down, /var/ct/* exists and may have user restored data in it. 

if [ -d /var/ct/orilIW/registry ]
then
   rm -rf /var/ct/IW/registry
   cp -rp /var/ct/orilIW/registry /var/ct/IW/registry
fi

rm -rf /var/ct/orilIW

# Call registry migration program - it assumes all RMC daemons  re down when invoked.
echo -n "running rhMigrate :" >> /opt/hsc/data/RMC_first_boot_completed
rhMigrate 2>&1 >> /opt/hsc/data/RMC_first_boot_completed	


# At this point, rsct is down and /var/ct/ is ready to go. 
# Now we want to ensure the ctrmc.acls file is ready for GA3. 
migrateRMCACL

# Enable remote RMC connections
echo -n "invoking rmcctrl -q :" >>/opt/hsc/data/RMC_first_boot_completed
rmcctrl -q 2>&1 >> /opt/hsc/data/RMC_first_boot_completed

# start rsct!
echo -n "invoking rmcctrl -A :" >>/opt/hsc/data/RMC_first_boot_completed
rmcctrl -A 2>&1 >> /opt/hsc/data/RMC_first_boot_completed

echo "lssrc -a output after configuration:" >>/opt/hsc/data/RMC_first_boot_completed
sleep 2
lssrc -a >> /opt/hsc/data/RMC_first_boot_completed

rm -f /opt/hsc/data/.setupRMC

echo "----------------------------------------" >> /opt/hsc/data/RMC_first_boot_completed
echo "$0 ran to completion on "`date` >>/opt/hsc/data/RMC_first_boot_completed
echo "----------------------------------------" >> /opt/hsc/data/RMC_first_boot_completed

exit 0
